package com.xjnt.base.support.admin.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Group;
import com.xjnt.base.support.admin.entity.Role;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.sql.SqlKit;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;

/**
 * @author caoyong
 *
 */
@Service
public class GroupService extends BaseService<Group> {

	@Override
	public Group getModel() {
		return Group.me;
	}
	
	public List<Group> findByIds(Object[] ids) {
		SqlCriteria criteria = new SqlCriteria(getModel());
		criteria.add(Restrictions.in(Group.PK_ID, ids));
		return getModel().find(criteria.toSql(), ids);
	}

	public List<Group> findByAccountId(String accountId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "sg");
		criteria.addRelation("sys_group_account", "sga");
		criteria.addRelation(Account.me, "acc");
		criteria.add(Restrictions.eqProp("sg.uuid", "sga.groupid"));
		criteria.add(Restrictions.eqProp("acc.uuid", "sga.accountid"));
		criteria.add(Restrictions.eq("acc.uuid", accountId));
		return getModel().find(criteria.toSql(), accountId);
	}
	
	public List<Group> findByOrgId(String orgId){
		SqlCriteria criteria = new SqlCriteria(getModel());
		criteria.add(Restrictions.eq(Group.FK_ORGID, orgId));
		return getModel().find(criteria.toSql(), orgId);
	}

	public boolean allocRole(String groupId, String roleId){
		int k = Db.update(SqlKit.findSql("Group.relationRole"), groupId, roleId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public void allocRoles(String groupId, List<Role> roles){
		for (Role role : roles) {
			Db.update(SqlKit.findSql("Group.relationRole"), groupId, role.getStr(Role.PK_ID));
		}
	}
	
	public boolean removeRole(String groupId, String roleId){
		int k = Db.update(SqlKit.findSql("Group.removeRole"), groupId, roleId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public void removeRoles(String groupId, List<Role> roles){
		for (Role role : roles) {
			Db.update(SqlKit.findSql("Group.removeRole"), groupId, role.getStr(Role.PK_ID));
		}
	}
	
	public boolean removeAllRole(String groupId){
		int k = Db.update(SqlKit.findSql("Group.removeAllRole"), groupId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean allocAccount(String groupId, String accountId){
		int k = Db.update(SqlKit.findSql("Group.relationAccount"), groupId, accountId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public void allocAccounts(String groupId, List<Account> accounts){
		for (Account account : accounts) {
			Db.update(SqlKit.findSql("Group.relationAccount"), groupId, account.getStr(Account.PK_ID));
		}
	}
	
	public void removeAccounts(String groupId, List<Account> accounts){
		for (Account account : accounts) {
			Db.update(SqlKit.findSql("Group.relationAccount"), groupId, account.getStr(Account.PK_ID));
		}
	}
	
	public boolean removeAllAccount(String groupId){
		int k = Db.update(SqlKit.findSql("Group.removeAllAccount"), groupId);
		if(k > 0){
			return true;
		}
		return false;
	}
}
